﻿using AutoMapper;
using MiniErp.Common.DB.MySqlDB;
using MiniErp.Entity.DataStreams.BOs;
using MiniErp.Entity.DataStreams.DOs.Notice;
using MiniErp.Entity.DataStreams.DTOs.Notice;
using MiniErp.Repository.BaseRepository;
using MiniErp.Repository.UnitOfWorkCore;
using MiniErp.Service.BusinessService.IService.Notice;
using MiniErp.Service.JWTService.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniErp.Service.BusinessService.Service.Notice
{
    public class UserInfoNoticesReplyService : BaseService, IUserInfoNoticesReplyService
    {
        /// <summary>
        /// 用户公告仓储
        /// </summary>
        private readonly BaseRepository<MysqlContext, UserInfoNoticesReply> _unrr;
        private readonly ITokenService _tokenService;
        public UserInfoNoticesReplyService(IMysqlUnitOfWork mysqlUnitOfWork, IMapper mapper, ITokenService tokenService) : base(mysqlUnitOfWork, mapper)
        {
            _unrr = _mysqlUnitOfWork.CreateRepository<MysqlContext, UserInfoNoticesReply>();
            _tokenService = tokenService;
        }

        #region 内部业务
        public int AddUserInfoNoticesReplys(List<AddUserInfoNoticesReplyDTO> addUserInfoNoticesReplyDTOs)
        {
            var userNoticesReply = _mapper.Map<List<UserInfoNoticesReply>>(addUserInfoNoticesReplyDTOs);
            _unrr.AddInfo(userNoticesReply);
            return addUserInfoNoticesReplyDTOs.Count;
        }

        public int DeleteUserInfoNoticesReplys(List<UserInfoNoticesReply> userNoticesId)
        {
            _unrr.DeleteInfo(userNoticesId);
            return userNoticesId.Count;
        }
        public Task<List<UserInfoNoticesReply>> GetUserInfoNoticesReplyByNoticesIdAsync(int noticesid)
        {
            return _unrr.GetInfoAsyncs(item => item.NoticesId == noticesid);
        }

        public Task<List<UserInfoNoticesReply>> GetUserInfoNoticesReplyByNoticesIdAndJobsIdAsync(int noticesid, int jobsid)
        {
            return _unrr.GetInfoAsyncs(item => item.NoticesId == noticesid && item.userInfo.JobsId == jobsid);
        }
        #endregion

        public async Task<List<UserInfoNoticesReplyDTO>> GetUserInfoNoticesReplyByNoticesIdAsync(int noticesid, int page, int num)
        {
            var userReply = await _unrr.GetInfoAsyncs<DateTime>(page, num, "ReplyTime", item => item.NoticesId == noticesid, false);
            var result = _mapper.Map<List<UserInfoNoticesReplyDTO>>(userReply);
            return result.Count == 0
                ? throw new ExceptionBO("未查询到数据")
                : result;
        }


        public async Task<List<UserInfoNoticesReplyDTO>> GetUserInfoNoticesReplyByUserIdAndTimeAsync(int uid, DateTime starttime, DateTime endtime, int page, int num)
        {
            var userReply = await _unrr.GetInfoAsyncs<DateTime>(page, num, "ReplyTime",
                item => (item.userInfoId == uid && item.ReplyTime >= starttime && item.ReplyTime <= endtime && item.IsReply == true)
                || (item.userInfoId == uid && item.IsReply == false)
                , false);
            return userReply.Count == 0
             ? throw new ExceptionBO("未查询到数据")
             : _mapper.Map<List<UserInfoNoticesReplyDTO>>(userReply);
        }

        public async Task<bool> UpdateUserInfoNoticesReplyAsync(UpdateUserInfoNoticesReplyDTO NoticesDTO)
        {
            var userReply = await _unrr.GetReadOnlyInfoAsync(item => item.UserNoticesId == NoticesDTO.UserNoticesId);
            if (userReply.userInfoId != _tokenService.GetUId()) throw new ExceptionBO("请不要非法操作数据，感谢");
            userReply.Reply = NoticesDTO.Reply;
            userReply.IsReply = true;
            userReply.ReplyTime = DateTime.Now;
            _unrr.UpdateInfo(userReply);
            var result = await _mysqlUnitOfWork.SaveAsync();
            return result == 0
                ? throw new ExceptionBO("回复失败")
                : true;
        }
    }
}
